package com.operation.controller;

import com.alibaba.fastjson.JSON;
import com.operation.beans.Result;
import com.operation.entity.UserInfo;
import com.operation.entity.WorkOrder;
import com.operation.service.UserService;
import com.operation.service.WorkOrderService;
import com.operation.utils.ResultUtil;
import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AssertionHolder;
import org.jasig.cas.client.validation.Assertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
public class UserServiceController {

    @Autowired
    UserService userService;
    @Autowired
    WorkOrderService workOrderService;

    /**
     * 用户注册
     * @param userInfo
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Result register(@RequestBody UserInfo userInfo){
        return userService.register(userInfo);
    }

    /**
     * 查看用户信息
     * @param userInfo
     */
    @RequestMapping(value = "/getUserInfo",method = RequestMethod.POST)
    public Result getUserInfo(@RequestBody UserInfo userInfo){
        return userService.getUserInfo(userInfo);
    }

    /**
     * 更新用户信息
     * @param userInfo
     */
    @RequestMapping(value = "/updateUserInfo",method = RequestMethod.POST)
    public Result updateUserInfo(@RequestBody UserInfo userInfo){
        UserInfo userInfo1 = new UserInfo();
        int code = 0;
        if (userService.updateUserInfo(userInfo)==1){
            userInfo1 = userInfo;
        }else{
            code = 1;
        }
        return ResultUtil.success(userInfo1,code);
    }

    /**
     * 添加用户
     * @param userInfo
     */
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public Result addUser(@RequestBody UserInfo userInfo){
        return ResultUtil.success(userService.add(userInfo),0) ;
    }
    /**
     * 判断用户是否存在
     * @param userAccount
     */
    @RequestMapping(value = "/isExist/{userAccount}",method = RequestMethod.GET)
    public int isExist(@PathVariable Long userAccount){
        return userService.isExist(userAccount);
    }

    /**
     * 用户提交工单
     * @param workOrder
     */
    @RequestMapping(value = "/workOrders",method = RequestMethod.POST)
    public Result addOneWorkOrder(@RequestBody WorkOrder workOrder){
        return workOrderService.addWorkOrder(workOrder);
    }

    /**
     * 用户追踪工单
     * @param jobNumber
     */
    @RequestMapping(value = "/workOrders/{jobNumber}",method = RequestMethod.GET)
    public Result queryOneWorkOrder(@PathVariable String jobNumber){
       return userService.queryWorkOrderStatus(jobNumber);
    }

    /**
     * 用户评论工单
     * @param workOrder
     */
    @RequestMapping(value = "/workOrders/review",method = RequestMethod.POST)
    public Result review(@RequestBody WorkOrder workOrder){
        return userService.review(workOrder);
    }


    /**
     * 用户查看已完成工单
     * @param userAccount
     */
    @RequestMapping(value = "/userWorkOrders/{userAccount}",method = RequestMethod.GET)
    public Result confirmCompleteWorKOrder(@PathVariable Integer userAccount){
        return userService.findHistoryWorkOrder(userAccount);
    }

    /**
     * 查看在线的运维人员
     */
    @RequestMapping(value = "/isOnline",method = RequestMethod.GET)
    public Result confirmCompleteWorKOrder(){
        return userService.isOnline();
    }

    /**
     * 得到教师信息
     */
    @RequestMapping(value = "/getTeacherInfo/{gonghao}",method = RequestMethod.GET)
    public Result getTeacherInfo(@PathVariable String gonghao) throws Exception {
        String path="http://172.16.10.80:8086/it.ashx?gh="+gonghao;
        String result = "";
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            if (conn.getResponseCode() == 200) {
                InputStream is = conn.getInputStream();
                String charset = "UTF-8";
                Pattern pattern = Pattern.compile("charset=\\S*");
                Matcher matcher = pattern.matcher(conn.getContentType());
                if (matcher.find()) {
                    charset = matcher.group().replace("charset=", "");
                }
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));

                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.success(result,0);
    }


    /**
     * 查看我的报修工单
     */
    @RequestMapping(value = "/findRepairOrder/{userAccount}",method = RequestMethod.GET)
    public Result findRepairOrder(@PathVariable Long userAccount){
        return ResultUtil.success(userService.findRepairOrder(userAccount),0);
    }

    /**
     * 投诉
     */
    @RequestMapping(value = "/complaint/{jobNumber}",method = RequestMethod.POST)
    public Result findRepairOrder(@PathVariable String jobNumber){
        return ResultUtil.success(userService.complaint(jobNumber),0);
    }


    @RequestMapping(value = "/updateTypeByUserName",method = RequestMethod.POST)
    public Result updateTypeByUserName(@RequestBody UserInfo userInfo){
        return ResultUtil.success(userService.updateTypeByUserName(userInfo),0);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public void logout() {
        Assertion assertion = AssertionHolder.getAssertion();
        AttributePrincipal ap = assertion.getPrincipal();
        UserInfo userInfo = new UserInfo();
        userInfo.setUserAccount(Long.valueOf(ap.getName()));
        userInfo.setUserPassword("0");
        if (userService.isIt(userInfo) != null) {
            userService.updateIsOnline(userInfo);
        }
    }
}
